package cn.xshi.oauth.web;

import cn.xshi.oauth.model.OauthTenant;
import cn.xshi.oauth.param.OauthAccountTenantParam;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.oauth.service.OauthAccountTenantService;
import cn.xshi.oauth.service.OauthTenantService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 授权中心租户
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/oauthTenant")
@Api(value = "租户",tags = "租户",description = "租户")
public class OauthTenantController extends BaseAction {

    @Resource
    OauthTenantService oauthTenantService;

    @Resource
    OauthAccountTenantService oauthAccountTenantService;

    /**
     * 查询子租户列表并分页
     * @param baseSearch
     */
    @ApiOperation(value="查询子租户列表并分页", notes="查询子租户列表并分页")
    @PostMapping(value="/list")
    @Auth(value = "/oauthTenant/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<OauthTenant>> getOauthTenantListByCondition(@RequestBody BaseSearch baseSearch){
        Map<String, Object> condition = baseSearch.convert();
        commonHPager(baseSearch);
        List<OauthTenant> oauthTenantList = oauthTenantService.getOauthTenantListByCondition(condition);
        for(OauthTenant oauthTenant : oauthTenantList){
//            if(!StringUtil.isEmpty(oauthTenant.getCreateId())){
//                OauthAccountEntity createBy = getAccount(oauthTenant.getCreateId());
//                if(null != createBy){
//                    oauthTenant.setCreateBy(createBy.getName());
//                }
//            }
//            if(!StringUtil.isEmpty(oauthTenant.getUpdateId())){
//                OauthAccountEntity modifiedBy = getAccount(oauthTenant.getUpdateId());
//                if(null != modifiedBy){
//                    oauthTenant.setModifiedBy(modifiedBy.getName());
//                }
//            }
            OauthAccountTenantParam oauthAccountTenantParam = new OauthAccountTenantParam();
            oauthAccountTenantParam.setTenantId(oauthTenant.getId());
            Integer count = oauthAccountTenantService.getOauthAccountTenantCount(oauthAccountTenantParam);
            if(count>0){
                oauthTenant.setBind(true);
            }
        }
        PageInfo<OauthTenant> page = new PageInfo<OauthTenant>(oauthTenantList);
        return outPageBootStr(page,baseSearch);
    }

    /**
     * 查询单个租户
     * @param id
     */
    @ApiOperation(value="查询单个租户", notes="查询单个租户")
    @GetMapping(value="/get/{id}")
    @Auth(value = "/oauthTenant/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<OauthTenant> getOauthTenantById(@PathVariable("id")String id){
        OauthTenant oauthTenant = oauthTenantService.getOauthTenantById(id);
        if(!StringUtil.isEmpty(oauthTenant.getCreateId())){
            OauthAccountEntity createBy = getAccount(oauthTenant.getCreateId());
            if(null != createBy){
                oauthTenant.setCreateBy(createBy.getName());
            }
        }
        if(!StringUtil.isEmpty(oauthTenant.getUpdateId())){
            OauthAccountEntity modifiedBy = getAccount(oauthTenant.getUpdateId());
            if(null != modifiedBy){
                oauthTenant.setModifiedBy(modifiedBy.getName());
            }
        }
        return outDataStr(oauthTenant);
    }

    /**
     * 添加
     * @param oauthTenant
     */
    @PostMapping(value="/add")
    @ApiOperation(value="创建单个租户", notes="创建单个租户")
    public BaseResult addOauthTenant(@RequestBody OauthTenant oauthTenant){
        int i = 0;
        if(null != oauthTenant){
            i=oauthTenantService.addOauthTenant(oauthTenant);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 修改
     * @param oauthTenant
     */
    @PutMapping(value="/update")
    @ApiOperation(value="编辑单个租户", notes="编辑单个租户")
    public BaseResult updateOauthTenant(@RequestBody OauthTenant oauthTenant){
        int i = 0;
        if(null != oauthTenant){
            i=oauthTenantService.updateOauthTenant(oauthTenant);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value="/delete")
    @ApiOperation(value="删除租户", notes="删除租户")
    public BaseResult delOauthTenant(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=oauthTenantService.delOauthTenant(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 根据账号类型查询租户
     * @param baseSearch
     */
    @ApiOperation(value="根据账号类型查询租户", notes="根据账号类型查询租户")
    @PostMapping(value="/tenantList")
    @Auth(value = "/oauthTenant/tenantList",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<OauthTenant>> getOauthTenantListByAccountTypeId(@RequestBody BaseSearch baseSearch){
        Map<String, Object> condition = baseSearch.convert();
        commonHPager(baseSearch);
        List<OauthTenant> oauthTenantList = oauthTenantService.getOauthTenantListByAccountTypeId(condition);
        PageInfo<OauthTenant> page = new PageInfo<OauthTenant>(oauthTenantList);
        return outPageBootStr(page,baseSearch);
    }
}
